﻿using System;
using System.Web;
using System.Net;

using BlueSky.Common;

namespace BlueSky.Web.Extentions
{
    public static class HttpRequestBaseExt
    {
        /// <summary>
        /// Receives value of parametre from HttpContext.Request.
        /// </summary>
        /// <param name="httpRequest">HTTP request.</param>
        /// <param name="name">Parametre name.</param>
        /// <returns>Value of parametre. Null-if the parametre is not found.</returns>
        public static string Get(this HttpRequestBase httpRequest, string name)
        {
            return httpRequest[name];
        }

        /// <summary>
        /// Receives value of parametre from HttpContext.Request with value by default.
        /// </summary>
        /// <param name="httpRequest">HTTP request.</param>
        /// <param name="name">Parametre name.</param>
        /// <param name="nullValue">Value if the parametre null (is not found).</param>
        /// <returns>Value of parametre. nullValue - if the parametre is not found.</returns>
        public static string Get(this HttpRequestBase httpRequest, string name, string nullValue)
        {
            string value = Get(httpRequest, name);
            return value.Equals(null) ? nullValue : value;
        }

        /// <summary>
        /// Receives the typified value of parametre from HttpContext.Request.
        /// </summary>
        /// <typeparam name="T">Parametre type.</typeparam>
        /// <param name="httpRequest">HTTP request.</param>
        /// <param name="name">Parametre name.</param>
        /// <returns>Value of parametre. null - if the parametre is not found.</returns>
        public static T Get<T>(this HttpRequestBase httpRequest, string name)
        {
            string value = Get(httpRequest, name);
            return (T)Convertors.ChangeType<T>(value);
        }

        /// <summary>
        /// Receives the typified value of parametre from HttpContext.Request.
        /// </summary>
        /// <typeparam name="T">Parametre type.</typeparam>
        /// <param name="httpRequest">HTTP request.</param>
        /// <param name="name">Parametre name.</param>
        /// <param name="formatInvalidValue">Value by default in a case if parametre does not correspond to type T.</param>
        /// <returns>Value of parametre. null - if the parametre is not found, formatInvalidValue - if the parametre does not correspond to type T.</returns>
        public static T Get<T>(this HttpRequestBase httpRequest, string name, T formatInvalidValue)
        {
            try
            {
                return Get<T>(httpRequest, name);
            }
            catch (FormatException)
            {
                return formatInvalidValue;
            }
        }

        /// <summary>
        /// Receives the typified value of parametre from HttpContext.Request.
        /// </summary>
        /// <typeparam name="T">Parametre type.</typeparam>
        /// <param name="httpRequest">HTTP request.</param>
        /// <param name="name">Parametre name.</param>
        /// <param name="formatInvalidValue">Value by default in a case if parametre does not correspond to type T.</param>
        /// <param name="nullValue">Value by default if the parametre is not found.</param>
        /// <returns>Value of parametre. nullValue - if the parametre is not found, formatInvalidValue - if the parametre does not correspond to type T.</returns>
        public static T Get<T>(this HttpRequestBase httpRequest, string name, T formatInvalidValue, T nullValue)
        {
            T value = Get<T>(httpRequest, name, formatInvalidValue);
            return value.Equals(null) ? nullValue : value;
        }

        /// <summary>
        /// Return user IP address.
        /// </summary>
        /// <param name="httpRequest">HTTP request.</param>
        /// <returns>User IP address.</returns>
        public static IPAddress GetUserIPAddress(this HttpRequestBase httpRequest)
        {
            IPAddress address;
            if (!IPAddress.TryParse(httpRequest.UserHostAddress, out address))
                address = null;            
            return address;
        }
    }
}
